<?php
	require_once 'Controller.php';
	require_once 'model/albumDAO.php';
	require_once 'model/imageDAO.php';
	
	class Album extends Controller {
		private $albumDAO;
		function __construct() {
			parent::__construct();
			$this->data->menu['Voir les photos'] = '?controller=Photo&action=index';
			$this->data->menu['Voir les albums'] = '?controller=Album&action=index';
			$this->albumDAO = new AlbumDAO();
			$this->imageDAO = new ImageDAO();
		}
		
		function index() {
			$this->data->view = 'albumMatrix.php';
			$this->data->albums = $this->albumDAO->getList();
		}
		
		function add() {
			$this->data->view = 'addAlbum.php';
			if (!empty($_POST)) {
				$this->albumDAO->save($_POST['name']);
				header('Location:?controller=Album&action=index');
			}
		}
		
		function view () {
			$this->data->view = 'albumView.php';
			if (!isset($_GET['aid']) OR $_GET['aid'] < 0)
				$_GET['aid'] = 1;
			$this->data->album = $this->albumDAO->getAlbum($_GET['aid']);
		}
		
		function addpict() {
			$this->data->view = 'addpict.php';
			$this->data->album = $this->albumDAO->getAlbum($_GET['aid']);
			if (isset($_GET['iid'])) {
				$img = $this->imageDAO->getImage($_GET['iid']);
				$this->data->album->addImg($img);
				$this->albumDAO->addImg($this->data->album->getId(), $img);
				header('Location: index.php?controller=Album&action=view&aid='.$_GET['aid']);
			}
			$this->data->imgs = $this->imageDAO->getImagesWithoutAlbum();
		}
	}